package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public JsonResult login(UserLoginParam userLoginParam) {
       UserVO userVO = userMapper.selectByUserName(userLoginParam.getUsername());
       if (userVO== null){//用户名不存在
           throw new ServiceException(StatusCode.USERNAME_ERROR);
       }
       if (!userVO.getPassword().equals(userLoginParam.getPassword())){ //密码错误
           throw new ServiceException(StatusCode.PASSWORD_ERROR);
       }
       return JsonResult.ok(userVO);
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if (user.getId()==null) {//新增用户
 //     判断用户名是否存在
            UserVO userVO = userMapper.selectByUserName(userSaveParam.getUsername());
            if (userVO != null) {//用户名已存在
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }
            user.setPassword("123456");
            user.setCreateTime(new Date());
            // user.setUpdateTime(new Date());
            userMapper.insert(user);
        }else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        return userMapper.selectUser(userQuery);
    }

    @Override
    public void resetPassword(Long userId) {
        User user = new User();
        // 修改密码直接创建一个新的User对象,不用先从数据库中查找
        user.setId(userId);
        user.setPassword("admin");
        userMapper.update(user);
    }

    @Override
    public void changeStatus(Long userId, String status) {
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        userMapper.update(user);
    }

    @Override
    public void deleteUserById(Long userId) {
        userMapper.deleteById(userId);
    }

    @Override
    public List<UserVO> selectAuditList(long parentId) {
        ArrayList<UserVO> userVOList = new ArrayList<>();
        // 根据上级用户Id将审批人查出,添加到List中
        UserVO auditUser1 = userMapper.selectById(parentId);
        userVOList.add(auditUser1);
        // 如果查到有上级,且上级还有上级
        if (auditUser1 !=null && auditUser1.getParentId()!=null){
        //     查询到上级,添加到List中
            UserVO auditUser2 = userMapper.selectById(auditUser1.getParentId());
            userVOList.add(auditUser2);
        }
        return userVOList;
    }
}
